package com.ccx.coachsite.ui.own;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.Account;
import com.ccx.coachsite.bus.AccountMgr;
import com.ccx.coachsite.bus.CommonBus;
import com.ccx.coachsite.bus.SharedKey;
import com.ccx.coachsite.util.SharedPreferencesUtil;
import com.ccx.coachsite.util.StringUtil;

public class OwerMoreScoreView extends OwerBaseView
{	
	private ImageView tmpView;
	private int mSelectIndex;
	private View scoreBtn1;
	private View scoreBtn2;
	private View scoreBtn3;
	private View scoreBtn4;
	private View scoreBtn5;
	private View scoreBtnSubmit;
	
	private ImageView tmpVipView;
	private int mVipSelectIndex;
	private View scoreVipBtn1;
	private View scoreVipBtn2;
	private View scoreVipBtn3;
	private View scoreVipBtn4;
	private View scoreVipBtn5;
	private Button scoreBtnVipSubmit;
	private String title;
	private int vipId;
	
	public OwerMoreScoreView(Context context, OwerMainBase superView) {
		super(context, superView);
		
		scoreBtn1 = findViewById(R.id.ower_more_btn_score1);
		scoreBtn1.setOnClickListener(new OnButtonListener());
		
		scoreBtn2 = findViewById(R.id.ower_more_btn_score2);
		scoreBtn2.setOnClickListener(new OnButtonListener());
		
		scoreBtn3 = findViewById(R.id.ower_more_btn_score3);
		scoreBtn3.setOnClickListener(new OnButtonListener());
		
		scoreBtn4 = findViewById(R.id.ower_more_btn_score4);
		scoreBtn4.setOnClickListener(new OnButtonListener());
		
		scoreBtn5 = findViewById(R.id.ower_more_btn_score5);
		scoreBtn5.setOnClickListener(new OnButtonListener());
		
		setImageViewById(R.id.ower_more_btn_score5);
		mSelectIndex = 5;
		
		scoreVipBtn1 = findViewById(R.id.ower_more_vip_btn_score1);
		scoreVipBtn1.setOnClickListener(new OnVipButtonListener());
		
		scoreVipBtn2 = findViewById(R.id.ower_more_vip_btn_score2);
		scoreVipBtn2.setOnClickListener(new OnVipButtonListener());
		
		scoreVipBtn3 = findViewById(R.id.ower_more_vip_btn_score3);
		scoreVipBtn3.setOnClickListener(new OnVipButtonListener());
		
		scoreVipBtn4 = findViewById(R.id.ower_more_vip_btn_score4);
		scoreVipBtn4.setOnClickListener(new OnVipButtonListener());
		
		scoreVipBtn5 = findViewById(R.id.ower_more_vip_btn_score5);
		scoreVipBtn5.setOnClickListener(new OnVipButtonListener());
		
		setVipImageViewById(R.id.ower_more_vip_btn_score5);
		mVipSelectIndex = 5;
		
		scoreBtnSubmit = findViewById(R.id.more_score_submit);
		scoreBtnSubmit.setOnClickListener(new OnScoreSubmitListener());
		
		scoreBtnVipSubmit = (Button)findViewById(R.id.more_vip_score_submit);
		scoreBtnVipSubmit.setOnClickListener(new OnScoreVipSubmitListener());
		
		boolean isExpert = SharedPreferencesUtil.getBoolean(mContext,
				SharedKey.Shared_Is_Expert);
		
		View vipPanel =  findViewById(R.id.ower_vip_sorce_panel);
		if(isExpert)
			vipPanel.setVisibility(View.GONE);
		
		Account acc = AccountMgr.readAccountInfo(mContext);
		vipId = acc.getExpertId();
		String name = acc.getExpertName();
		if(!StringUtil.isEmpty(name))
		{
			((TextView) findViewById(R.id.ower_more_vip_title_name))
			.setText(name);
		}
		else
		{
			vipId = -1;
			//scoreBtnVipSubmit.setEnabled(false);
			//scoreBtnVipSubmit.setTextColor(mContext.getResources().getColor(R.color.dark_gray));
		}
//		if (isExpert) {
//			title = "专家";
//		} else {
			title = "助理";
//		}
		((TextView) findViewById(R.id.ower_more_vip_title))
		.setText("给"+title+"评分");
		((TextView) findViewById(R.id.ower_more_vip_title_desc))
		.setText("您当前"+title+":");
		
		
    }   
	
	class OnScoreSubmitListener implements OnClickListener {
		@Override
		public void onClick(View v) 
		{
			CommonBus bus = new CommonBus(mContext);
			bus.setScoreParam(mSelectIndex);
			bus.doTask();
		}
	}
	
	class OnScoreVipSubmitListener implements OnClickListener {
		@Override
		public void onClick(View v) 
		{
			if(vipId == -1)
			{
				String info = "您目前没有"+title+"暂时无法评分。";
				Toast.makeText(mContext, info, Toast.LENGTH_SHORT).show();
				return;
			}
			CommonBus bus = new CommonBus(mContext);
			bus.setVipScoreParam(vipId, mVipSelectIndex);
			bus.doTask();
		}
	}
	
	private void setImageViewById(int id)
	{
		ImageView img = null;
		switch(id)
		{
			case R.id.ower_more_btn_score1:
				img = (ImageView)findViewById(R.id.ower_more_img_score1);
				mSelectIndex = 1;
				break;
			case R.id.ower_more_btn_score2:
				img = (ImageView)findViewById(R.id.ower_more_img_score2);
				mSelectIndex = 2;
				break;
			case R.id.ower_more_btn_score3:
				img = (ImageView)findViewById(R.id.ower_more_img_score3);
				mSelectIndex = 3;
				break;
			case R.id.ower_more_btn_score4:
				img = (ImageView)findViewById(R.id.ower_more_img_score4);
				mSelectIndex = 4;
				break;
			case R.id.ower_more_btn_score5:
				img = (ImageView)findViewById(R.id.ower_more_img_score5);
				mSelectIndex = 5;
				break;
			
				default:
					break;
		}
		
		if(tmpView != null)
		{
			tmpView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ower_image_signal_unchecked));
		}
		
		if(img != null)
		{
			img.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ower_image_signal_checked));
			tmpView = img;
		}
	}
	
	class OnButtonListener implements OnClickListener {
		@Override
		public void onClick(View v) 
		{
			setImageViewById(v.getId());
		}
	}
	
	private void setVipImageViewById(int id)
	{
		ImageView img = null;
		switch(id)
		{
			case R.id.ower_more_vip_btn_score1:
				img = (ImageView)findViewById(R.id.ower_more_vip_img_score1);
				mVipSelectIndex = 1;
				break;
			case R.id.ower_more_vip_btn_score2:
				img = (ImageView)findViewById(R.id.ower_more_vip_img_score2);
				mVipSelectIndex = 2;
				break;
			case R.id.ower_more_vip_btn_score3:
				img = (ImageView)findViewById(R.id.ower_more_vip_img_score3);
				mVipSelectIndex = 3;
				break;
			case R.id.ower_more_vip_btn_score4:
				img = (ImageView)findViewById(R.id.ower_more_vip_img_score4);
				mVipSelectIndex = 4;
				break;
			case R.id.ower_more_vip_btn_score5:
				img = (ImageView)findViewById(R.id.ower_more_vip_img_score5);
				mVipSelectIndex = 5;
				break;
			
				default:
					break;
		}
		
		if(tmpVipView != null)
		{
			tmpVipView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ower_image_signal_unchecked));
		}
		
		if(img != null)
		{
			img.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ower_image_signal_checked));
			tmpVipView = img;
		}
	}
	
	class OnVipButtonListener implements OnClickListener {
		@Override
		public void onClick(View v) 
		{
			setVipImageViewById(v.getId());
		}
	}
}
